// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EnumExtensions.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Extensions that help with enum types.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Diagnostics.CodeAnalysis;

namespace AzureContrib.WindowsAzure
{
	/// <summary>
	/// Extensions that help with enum types.
	/// </summary>
	public static class EnumExtensions
	{
		/// <summary>
		/// Convert any object to an enum of type T using .ToString().
		/// </summary>
		/// <typeparam name="T">The type of enum to convert to.</typeparam>
		/// <param name="enumerableInstance">The instance to convert to the enum.</param>
		/// <returns>The enum.</returns>
		/// <remarks>Quite obvkously this fails in a BAD way if the in-object is not convertable to the enum type.</remarks>
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		public static T ToEnum<T>(this object enumerableInstance)
		{
			Assumes.NotNull(enumerableInstance, "enumerableInstance");
			return (T)Enum.Parse(typeof(T), enumerableInstance.ToString());
		}
	}
}